package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalDiscountSetting;
import com.moleq.models.GlobalInfo;
import com.moleq.models.SetFunction;

public class GlobalDiscountSettingService {
	private DatabaseHelper DBHelper;
	private String errorString;
	private Context mContext;

	public GlobalDiscountSettingService() {

	}

	public GlobalDiscountSettingService(Context context) {
		mContext = context;
		this.DBHelper = new DatabaseHelper(GlobalInfo.getInstance().ShareContext);
	}
	
	public List<GlobalDiscountSetting> FindAll(){
		List<GlobalDiscountSetting> obj = new ArrayList<GlobalDiscountSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME +" order by gd_no",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int gd_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_NO));
			String gd_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_DESC));
			double gd_value  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_VALUE));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.UPDATE_TIME));
			
			GlobalDiscountSetting gd = new GlobalDiscountSetting(gd_no, gd_desc, gd_value, create_datetime, update_datetime);
			gd.setType("G");
			obj.add(gd);
		}
		cursor.close();
		return obj;
	}
	
	public List<SetFunction> FindGlobalDisNoDesc(){
		List<SetFunction> obj = new ArrayList<SetFunction>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select gd_no,gd_desc from "+ DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME +" order by gd_no",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		obj.add(0, new SetFunction("-1", "Remove"));
		obj.add(new SetFunction("0", "Manual"));
		while(cursor.moveToNext()){
			String gd_no  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_NO));
			String gd_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_DESC));
			
			obj.add(new SetFunction(gd_no, gd_desc));
		}
		cursor.close();
		return obj;
	}
	
	public GlobalDiscountSetting Find(int no){
		GlobalDiscountSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME +" where gd_no = "+no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int gd_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_NO));
			String gd_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_DESC));
			double gd_value  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_VALUE));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.UPDATE_TIME));
			
			obj = new GlobalDiscountSetting(gd_no, gd_desc, gd_value, create_datetime, update_datetime);
		}
		cursor.close();
		return obj;
	}
	
	public boolean updateSetGlobalDiscountNo(int discount_no){
		boolean check = true;
		errorString = DBHelper.execSQL("Update "+ DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME +" set gd_no = gd_no - 1 where gd_no > " + discount_no);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean insertGlobalDiscountSetting(GlobalDiscountSetting obj){
		boolean check = true;
		String sql,desc;
		double value;
		desc = obj.getGd_desc();
		value = obj.getGd_value();
		
		sql = "Insert into "+ DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME +" values";
		sql += "(null,'"+ desc +"',"+ value +",strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean updateGlobalDiscountSetting(GlobalDiscountSetting obj){
		boolean check = true;
		String sql,desc;
		double value;
		int no = obj.getGd_no();
		desc = obj.getGd_desc();
		value = obj.getGd_value();
		
		sql = "UPDATE "+ DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME +" set ";
		sql += "gd_desc = '"+ desc +"',gd_value = "+ value +"," +
				"update_datetime = strftime('%Y%m%d%H%M%S','now') where gd_no = "+ no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean deleteGlobalDiscountSetting(int no){
		boolean check = true;
		String sql = "delete from "+ DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME +" where gd_no = " + no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public String getGlobalDiscountName(String no){
		String obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select gd_desc from "+ DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME +" where gd_no = "+no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		if(cursor.moveToNext()){
			obj = cursor.getString(cursor.getColumnIndex(DatabaseSchema.GLobalDiscountSetting.GD_DESC));
		}
		cursor.close();
		return obj;
	}
	
	public boolean FuncPanelHaveGlobalDiscount(int globalNo){
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select tp_parameter2 from t_function_panel where tp_func_key = 24 and tp_parameter2 = " + globalNo, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return result;
		}
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		return result;
	}
	
	public int getMaxGlobalNo(){
		int order = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select Max(gd_no) from " + DatabaseSchema.GLobalDiscountSetting.SCHEMA_NAME, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return order;
		}
		if (cursor.moveToNext()) {
			order = cursor.getInt(0);
		}
		cursor.close();
		return order;
	}
}
